﻿using Model;
using System;
using System.Data;

namespace BLL
{
    public class Qs_Their_MoneyBLL
    {
        private OracleHelper helper = new OracleHelper();
        private Qs_SequencesBLL seqBll = new Qs_SequencesBLL();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <param name="logonName"></param>
        /// <returns></returns>
        public int InsertModel(Qs_their_money model, string logonName)
        {
            //SQL 语句
            string sql = string.Format(@"insert into Qs_their_money
	(
n_their_money_id,
			 cmy_id,
			 n_their_id,
			 c_their_name,
			 n_weekly_plan_last,
			 n_usage,
			 n_weekly_plan_this,
			 c_remark,
			 user_id,
			 c_month,C_NUM)
        values
		(n_their_money_id.nextval," + model.cmy_id + "," + model.n_their_id + ",'" + model.c_their_name + "'," + model.n_weekly_plan_last + "," + model.n_usage + @",'"
                                    + model.n_weekly_plan_this + @"','"
                                    + model.c_remark + "'," + model.user_id + ",to_date('" + model.c_month + "','yyyy-MM-dd HH24:Mi:ss')," + model.c_num + ")");
            //执行sql并接收返回结果

            Qs_their_money thm = null;
            int count = 0;
            try
            {
                count = helper.ExecuteDataBase(sql, logonName);

                if (count > 0)
                {
                    thm = new Qs_their_money();
                    sql = "";
                    DataTable dt = helper.GetDataTable(sql);
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow item in dt.Rows)
                        {
                        }
                    }
                }
                return count;
            }
            catch (Exception)
            {
                return count;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="logonName"></param>
        /// <returns></returns>
        public int UpdateModelNum()
        {
            int count = -1;

            //SQL 语句
            string sql = string.Format(@"update qs_their_money th set th.c_num=(select max(t.c_num)+1 from qs_their_money t) where th.c_num=0 ");
            //执行sql并接收返回结果

            try
            {
                int num = helper.ExecuteDataBase(sql, "");

                if (num > 0)
                {
                    sql = "select max(t.c_num) from qs_their_money t";
                    DataTable dt = helper.GetDataTable(sql);
                    if (dt.Rows.Count > 0 && dt != null)
                    {
                        foreach (DataRow item in dt.Rows)
                        {
                            count = int.Parse(item[0].ToString());
                        }
                    }
                }
            }
            catch (Exception)
            {
                return count;
            }
            return count;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="comId"></param>
        /// <returns></returns>
        public DataTable SelectModelNum(string num)
        {
            //SQL 语句
            string sql = string.Format(@"select th.n_their_id, th.n_total,
       th.n_produce,
       th.n_business_name,
       th.n_business,
       th.n_value,
       thm.c_their_name,
       thm.n_weekly_plan_last,
       thm.n_usage,
       thm.n_weekly_plan_this,
       thm.c_remark,
       co.cmy_name,
       u.user_name, t.n_business busId
  from qs_their_money thm, qs_their th,qs_their_moneytotal t,
   sys_user u, sys_company co
 where u.user_id = thm.user_id
   and thm.n_their_id = th.n_their_id and t.c_num=thm.c_num
   and co.cmy_id = thm.cmy_id
   and t.c_num in( " + num);
            sql += ") order by thm.n_their_money_id";
            //执行sql并接收返回结果

            DataTable dt = helper.GetDataTable(sql);

            return dt;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="logonName"></param>
        /// <returns></returns>
        public int UpdateModel(Qs_their_money model, string logonName)
        {
            //SQL 语句
            string sql = string.Format(@"");
            //执行sql并接收返回结果

            int count = 0;
            try
            {
                count = helper.ExecuteDataBase(sql, logonName);

                return count;
            }
            catch (Exception)
            {
                return count;
            }
        }
    }
}